package com.powerteam.service.crm.impl;

import com.alibaba.fastjson.JSON;
import com.powerteam.exception.BusinessException;
import com.powerteam.mapper.crm.ShareGroupMapper;
import com.powerteam.model.crm.ShareGroup;
import com.powerteam.service.crm.ShareGroupService;
import com.powerteam.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 共享资源相关服务接口实现
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Service
public class ShareGroupServiceImpl implements ShareGroupService {


    private static final String CLASS_NAME = "ShareGroupServiceImpl";

    @Autowired
    private ShareGroupMapper shareGroupMapper;

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result insert(ShareGroup shareGroup) {

        try {

            // 创建共享组信息
            boolean result = shareGroupMapper.insertShareGroup(shareGroup) > 0;
            log.info("{} insert 创建共享组信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(shareGroup), result);

            return new Result(result);
        } catch (Exception e) {
            log.info("{} insert 创建共享组信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(shareGroup));
            log.error(e.getMessage(), e);
            throw new BusinessException("创建共享组信息失败");
        }
    }

    @Override
    @Transactional(rollbackFor = {BusinessException.class}, propagation = Propagation.REQUIRED)
    public Result deleteByResource(ShareGroup shareGroup) {

        try {

            // 根据资源信息删除资源组信息
            boolean result = shareGroupMapper.deleteByResource(shareGroup) > 0;
            log.info("{} deleteByResource 根据资源信息删除资源组信息 入参0:{} 结果0:{}", CLASS_NAME, JSON.toJSON(shareGroup), result);

            return new Result(result);
        } catch (Exception e) {
            log.warn("{} deleteByResource 根据资源信息删除资源组信息异常 入参0:{}", CLASS_NAME, JSON.toJSON(shareGroup));
            log.info(e.getMessage(), e);
            throw new BusinessException("删除资源组信息失败");
        }
    }
}
